package com.taotao.search.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;

/**
 * 从索引库中搜索商品的DAO
 * @title SearchDao
 * <p>description</p>
 * <p>company: www.itheima.com</p>
 * @author 天火
 * @version 1.0
 */
@Repository
public class SearchDao {

	@Autowired
	private SolrServer solrServer;
	
	@Autowired
	private SearchItemMapper mapper;
	
	/**
	 * 
	 * @param query
	 * @return
	 * @throws Exception
	 */
	public SearchResult search(SolrQuery query) throws Exception{
		
		SearchResult searchResult = new SearchResult();
		//1.搜索solrserver对象由spring管理注入
		//2.直接执行查询
		QueryResponse qResponse = solrServer.query(query);
		//3.获取结果集
		SolrDocumentList results =  qResponse.getResults();
		searchResult.setRecordCount(results.getNumFound());
		//4.遍历结果集
		List<SearchItem> searchItems = new ArrayList<>();
		
		//取高亮
		Map<String, Map<String, List<String>>> highLighTing = qResponse.getHighlighting();
		
		for (SolrDocument document : results) {
			//将document中的值一个个复制到searchItem中
			SearchItem searchItem = new SearchItem();
			searchItem.setCategory_name(document.get("item_category_name").toString());
			searchItem.setId(Long.parseLong(document.get("id").toString()));
			searchItem.setImage(document.get("item_image").toString());
			searchItem.setPrice((Long)document.get("item_price"));
			searchItem.setSell_point(document.get("item_sell_point").toString());
			//取高亮
			List<String > list = highLighTing.get(document.get("id")).get("item_title");
			//判断list是否为空
			String gaoLiangs = "";
			
			if(list!=null && list.size()!=0) {
				gaoLiangs = list.get(0);
			}else {
				gaoLiangs = document.get("item_title").toString();
			}
			
			searchItem.setTitle(gaoLiangs);
			//searchItem.setItem_desc(item_desc);
			//将searchItem封装到results中的itemLIst中
			searchItems.add(searchItem);
		}
		//5.设置SearchResult的属性
		searchResult.setItemList(searchItems);
		return searchResult;
	}
	
	/**
	 * 更新索引库
	 * @param itemId
	 * @return
	 */
	public TaotaoResult updateSearchItemById(Long itemId) throws Exception{
		//注入mapper
		//查询到记录
		SearchItem searchItem = mapper.getSearchItemById(itemId);
		//把记录更新到索引库
			//创建solrServer
			//创建solrinputdocument 对象
			SolrInputDocument solrInputDocument = new SolrInputDocument();
			//向文档对象中添加域
			solrInputDocument.addField("id", searchItem.getId().toString());
			solrInputDocument.addField("item_title", searchItem.getTitle());
			solrInputDocument.addField("item_sell_point", searchItem.getSell_point());
			solrInputDocument.addField("item_price", searchItem.getPrice());
			solrInputDocument.addField("item_image", searchItem.getImage());
			solrInputDocument.addField("item_category_name", searchItem.getCategory_name());
			//添加文档
			solrServer.add(solrInputDocument);
			
			//提交
			solrServer.commit();
		
		
		
		
		
		return TaotaoResult.ok();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
